package com.dataaccess.dataaccess.web.web;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseController;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.ByteUtil;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.DeviceAboatFirewaterService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * 建筑消防用水--设备控制器
 */
@Action
@RequestMapping("firewater")
public class DeviceAboatFirewaterController extends BaseController {

    @Resource
    public MyLogger logger;
    @Resource
    public DeviceAboatFirewaterService deviceAboatFirewaterService;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;
    @Resource
    UserProducer userProducer;

    /**
     * 订阅--调用相关接口
     * @param
     * @return
     */
    @At(value = "device/getInfo", method = RequestMethod.POST)
    public RespEntity getInfo(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        return deviceAboatFirewaterService.getInfo(jsonParam);
    }

    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceInfoChanged", method = RequestMethod.POST)
    public RespEntity deviceInfoChanged(@RequestBody(required = false) JSONObject jsonParam) {
        return deviceAboatFirewaterService.deviceInfoChanged(jsonParam);
    }

    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceDataChanged", method = RequestMethod.POST)
    public RespEntity deviceDataChanged(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("firewaterdeviceDataChanged"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        jsonParam.put("deviceFlag","firewater");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return deviceAboatFirewaterService.deviceDataChanged(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 世成电器lora水设备
     * @Date Created in 11:18 2021/11/24
     * @Param [jsonParam]
     */

    @At(value = "device/deviceDataChanged/lora", method = RequestMethod.POST)
    public RespEntity scdqLORA(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("SCDQLORA"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        //--------------------解析数据--------------------------------------------
        String code = jsonParam.getStr("code");
        //返回指令
        String returnCode = "";
        String number = code.substring(4,8);
        returnCode = "5A5A"+number+"0500";
        String crc = ByteUtil.getCRC(ByteUtil.hex2byte(returnCode));
        returnCode =returnCode+crc.substring(2,4)+crc.substring(0,2)+"A5A5";
        jsonParam.put("deviceFlag","SCDQLORA");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            deviceAboatFirewaterService.scdqLORA(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success(returnCode);
    }

    /**
     * @Author YangYu
     * @Description  世成电器水设备（电信）
     * @Date Created in 11:13 2023/2/4
     * @Param [jsonParam]
     */

    @At(value = "device/deviceDataChanged/shichengWaterDX", method = RequestMethod.POST)
    public RespEntity shichengDX(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("shichengWaterDX"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("shichengWaterDX");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        jsonParam.put("deviceFlag","shichengWaterDX");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return deviceAboatFirewaterService.shichengDX(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description  世成电器水设备（电信新协议）
     * @Date Created in 11:13 2023/2/4
     * @Param [jsonParam]
     */

    @At(value = "device/deviceDataChanged/shichengDX_new", method = RequestMethod.POST)
    public RespEntity shichengDX_new(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("shichengDX_new"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("shichengDX_new");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        jsonParam.put("deviceFlag","shichengDX_new");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return deviceAboatFirewaterService.shichengDX_new(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }


    /**
     * @Author YangYu
     * @Description 无线仪表（京电云@正润NB（电信））
     * @Date Created in 17:10 2024/1/15
     * @Param [jsonParam]
     */

    @At(value = "device/deviceDataChanged/wirelessInstrument", method = RequestMethod.POST)
    public RespEntity wirelessInstrument(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("wirelessInstrument"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("wirelessInstrument");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        jsonParam.put("deviceFlag","wirelessInstrument");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return deviceAboatFirewaterService.wirelessInstrument(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }


}
